package tables

import (
	"peilian-api/app/global/variable"
	"time"
)

type SpecialRecord struct {
	Model
	UserID     uint      `json:"user_id" gorm:"type:uint"`
	Status     uint      `gorm:"type:uint;default:0;comment:0:创建专项练习题目 1:面试完成 2:回答完成，打分中 3:面试中"`
	FinishAt   time.Time `gorm:"comment:面试结束时间"`
	TrainAt    time.Time `gorm:"comment:面试开始时间"`
	CareerID   uint
	QuestionID uint
	Question   *Question

	AnswerText  string  `gorm:"comment:回答的asr文本"`
	TotalScore  float64 `gorm:"type:float;comment:报告总分"`
	Score       float64 `gorm:"type:decimal(5,2);default:0.00;comment:评分"`
	SpeakTime   uint    `gorm:"default:0; comment:文本时长(s),四舍五入时间"`
	AnswerAudio string  `gorm:"type:varchar(255);comment:回答的音频地址"`
	Audio       string  `gorm:"type:varchar(255);comment:音频mp3格式"`
	// 问题信息
	Criteria             string  `json:"criteria" gorm:"type:varchar(20);comment:考察能力"`
	QuestionText         string  `gorm:"type:longtext;comment:问题文本"`
	CareerName           string  `gorm:"type:varchar(50);comment:题库名称"`
	Level                string  `gorm:"type:varchar(50);comment:问题类型[基础 进阶]"`
	AnsAnalysis          string  `json:"ans_analysis" gorm:"comment:答案解析参考"`
	MainPointScore       float64 `gorm:"type:decimal(5,2);default:0.00;comment:要点覆盖率"`
	MainPointScoreDetail string  `gorm:"comment:要点评分详细信息"`
	TransStatus          int     `gorm:"type:tinyint(3);default:0;comment:PCM转MP3语音转换状态 0:待翻译 1:转换完成 2:转换中"`

	// 用户具体信息
	School     string `json:"school" gorm:"type:varchar(100);comment:学校"`
	Academy    string `json:"academy" gorm:"type:varchar(255);comment:学院"`
	Profession string `json:"profession" gorm:"type:varchar(255);comment:专业"`
	Grade      string `gorm:"type:varchar(50);comment:年级"`
	UserName   string `gorm:"type:varchar(50);comment:用户名"`
	UserPhone  string `json:"phone" gorm:"type:varchar(20);comment:手机"`
	Education  string `gorm:"type:varchar(10);default:\"\";comment:学历"`
	Duration   uint   `gorm:"type:uint;default:0;comment:训练时长"`
	Type       string `gorm:"type:varchar(50);comment:用户类型,同user表中type"`
	IsHelp     uint   `gorm:"type:uint;default:0;comment:训练时长"`

	QuestionOrigin  string `json:"question_origin" gorm:"type:varchar(512);comment:题目来源"`
	AnsAnalysisTag  string `json:"ans_analysis_tag" gorm:"type:varchar(512);comment:解析tag"`
	PartLabelsCache string `json:"part_labels_cache" gorm:"type:longtext;comment:每道题提取标签词的结果缓存，用于最终标签词生成"`
	CriteriaTag     string `json:"criteria_tag" gorm:"type:varchar(512);comment:能力标签(专项练习算法使用)"`
	Evaluation      string `gorm:"comment:v1.0算法评测结果"`

	// 评分
	AsrScore             float64 `gorm:"type:decimal(5,2);default:0.00;comment:asr字数评分(含标点)"`
	LanguageFluencyScore float64 `gorm:"type:decimal(5,2);default:0.00;comment:语言流畅度(asr*0.5+自信度*0.5)"`
	LanguageScore        float64 `gorm:"type:decimal(5,2);default:0.00;comment:语言表述能力评分(语言流畅度+语调气场)/2"`
	ThemeScore           float64 `gorm:"type:decimal(5,2);default:0.00;comment:主题评分"`
	Mood                 float64 `gorm:"type:decimal(5,2);default:0.00;comment:语气助词多寡(自信度)"`
	Tone                 float64 `gorm:"type:decimal(5,2);default:0.00;comment:语调气场"`
	TopicScore           float64 `gorm:"type:decimal(5,2);default:0.00;comment:题目评测得分(语言表述能力*0.2+围绕主题*0.3+要点覆盖*0.5)"`
	LogicScore           float64 `gorm:"type:decimal(5,2);default:0.00;comment:逻辑思维评测得分"`
}

func (r *SpecialRecord) TableName() string {
	return "special_record"
}

// 创建记录
func (r *SpecialRecord) Create(sr SpecialRecord) (SpecialRecord, error) {
	d := variable.DB.MySQL.Model(&r).Create(&sr)
	if d.Error != nil {
		return sr, d.Error
	}
	return sr, nil
}

// 根据条件查询list
func (r *SpecialRecord) GetSpecialListByWhere(whereMap map[string]interface{}, whereStr string, slt string, order string, page int, size int) ([]SpecialRecord, int64, error) {
	if len(slt) == 0 {
		slt = "*"
	}
	var count int64 = 0
	var out []SpecialRecord
	if len(whereMap) != 0 && len(whereStr) != 0 && len(order) != 0 && page >= 0 && size > 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Where(whereStr).Preload("Question").Select(slt).Order(order).Offset((page - 1) * size).Limit(size).Find(&out).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, 0, d.Error
		}
	} else if len(whereStr) != 0 && len(order) != 0 && page >= 0 && size > 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereStr).Preload("Question").Select(slt).Order(order).Offset((page - 1) * size).Limit(size).Find(&out).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, 0, d.Error
		}
	} else if len(whereMap) != 0 && len(order) != 0 && page >= 0 && size > 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Preload("Question").Select(slt).Order(order).Offset((page - 1) * size).Limit(size).Find(&out).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, 0, d.Error
		}
	} else if len(whereMap) != 0 && len(whereStr) != 0 && page >= 0 && size > 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Where(whereStr).Preload("Question").Select(slt).Offset((page - 1) * size).Limit(size).Find(&out).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, 0, d.Error
		}
	} else if len(whereMap) != 0 && len(whereStr) != 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Where(whereStr).Preload("Question").Select(slt).Find(&out).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, 0, d.Error
		}
	} else if len(whereMap) != 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Preload("Question").Select(slt).Find(&out).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, 0, d.Error
		}
	} else if len(whereStr) != 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereStr).Preload("Question").Select(slt).Find(&out).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, 0, d.Error
		}
	}
	return out, count, nil
}

// 更新数据
func (r *SpecialRecord) UpdateSpecial(whereMap map[string]interface{}, whereStr string, updateMap map[string]interface{}, limit int) (int64, error) {
	if len(whereMap) != 0 && len(whereStr) != 0 && len(updateMap) != 0 && limit > 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Where(whereStr).Updates(updateMap).Limit(limit)
		if d.Error != nil {
			return d.RowsAffected, d.Error
		}
		return d.RowsAffected, nil
	} else if len(whereMap) != 0 && len(whereStr) != 0 && len(updateMap) != 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Where(whereStr).Updates(updateMap)
		if d.Error != nil {
			return d.RowsAffected, d.Error
		}
		return d.RowsAffected, nil
	} else if len(whereMap) != 0 && len(updateMap) != 0 && limit > 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Updates(updateMap).Limit(limit)
		if d.Error != nil {
			return d.RowsAffected, d.Error
		}
		return d.RowsAffected, nil
	} else if len(whereStr) != 0 && len(updateMap) != 0 && limit > 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereStr).Updates(updateMap).Limit(limit)
		if d.Error != nil {
			return d.RowsAffected, d.Error
		}
		return d.RowsAffected, nil
	} else if len(whereMap) != 0 && len(updateMap) != 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereMap).Updates(updateMap)
		if d.Error != nil {
			return d.RowsAffected, d.Error
		}
		return d.RowsAffected, nil
	} else if len(whereStr) != 0 && len(updateMap) != 0 {
		d := variable.DB.MySQL.Model(&r).Where(whereStr).Updates(updateMap)
		if d.Error != nil {
			return d.RowsAffected, d.Error
		}
		return d.RowsAffected, nil
	}
	return -1, nil
}
